home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 39 / Amiga Format CD39 (1999-04-13)(Future Publishing)(GB)[!][issue 1999-05].iso / -seriously_amiga- / graphics / mpeg2decode / src / mpeg2dec.c < prev    next >
C/C++ Source or Header  |  1999-03-02  |  21KB  |  857 lines

  1.  
  2. /* mpeg2dec.c, main(), initialization, option processing                    */
  3.  
  4. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  5.  
  6. /*
  7.  * Disclaimer of Warranty
  8.  *
  9.  * These software programs are available to the user without any license fee or
  10.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  11.  * any and all warranties, whether express, implied, or statuary, including any
  12.  * implied warranties or merchantability or of fitness for a particular
  13.  * purpose.  In no event shall the copyright-holder be liable for any
  14.  * incidental, punitive, or consequential damages of any kind whatsoever
  15.  * arising from the use of these programs.
  16.  *
  17.  * This disclaimer of warranty extends to the user of these programs and user's
  18.  * customers, employees, agents, transferees, successors, and assigns.
  19.  *
  20.  * The MPEG Software Simulation Group does not represent or warrant that the
  21.  * programs furnished hereunder are free of infringement of any third-party
  22.  * patents.
  23.  *
  24.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  25.  * are subject to royalty fees to patent holders.  Many of these patents are
  26.  * general enough such that they are unavoidable regardless of implementation
  27.  * design.
  28.  *
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <ctype.h>
  34. #include <fcntl.h>
  35.  
  36. #define GLOBAL2
  37. #include "config.h"
  38. #include "global.h"
  39.  
  40. #include<exec/types.h>
  41. #include<graphics/modeid.h>
  42.  
  43. /* private prototypes */
  44. static int  video_sequence _ANSI_ARGS_((int *framenum));
  45. static int Decode_Bitstream _ANSI_ARGS_((void));
  46. static int  Headers _ANSI_ARGS_((void));
  47. static void Initialize_Sequence _ANSI_ARGS_((void));
  48. static void Initialize_Decoder _ANSI_ARGS_((void));
  49. static void Deinitialize_Sequence _ANSI_ARGS_((void));
  50. static void Process_Options _ANSI_ARGS_((int argc, char *argv[]));
  51.  
  52. extern int Decode_Audio();
  53. int wb=0;
  54. #if OLD
  55. static int  Get_Val _ANSI_ARGS_((char *argv[]));
  56. #endif
  57.  
  58. /* #define DEBUG */
  59.  
  60. static void Clear_Options();
  61. #ifdef DEBUG
  62. static void Print_Options();
  63. #endif
  64.  
  65. FILE *pipe_file;
  66.  
  67. #ifdef STORM
  68. int main(int argc,char *argv[])
  69. #else
  70. int main(argc,argv)
  71. int argc;
  72. char *argv[];
  73. #endif
  74. {
  75.   int ret, code;
  76.  
  77.   Clear_Options();
  78.  
  79.   /* decode command line arguments */
  80.   Process_Options(argc,argv);
  81.  
  82. #ifdef DEBUG
  83.   Print_Options();
  84. #endif
  85.  
  86.   ld = &base; /* select base layer context */
  87.  
  88.   /* open MPEG base layer bitstream file(s) */
  89.   /* NOTE: this is either a base layer stream or a spatial enhancement stream */
  90.   if ((base.Infile=fopen(Main_Bitstream_Filename,"r"))<0)
  91.   {
  92.     fprintf(stderr,"Base layer input file %s not found\n", Main_Bitstream_Filename);
  93.     exit(1);
  94.   }
  95.  
  96.  
  97.   if(base.Infile != 0)
  98.   {
  99.     Initialize_Buffer(); 
  100.   
  101.     if(Show_Bits(8)==0x47)
  102.     {
  103.       sprintf(Error_Text,"Decoder currently does not parse transport streams\n");
  104.       Error(Error_Text);
  105.     }
  106.  
  107.     next_start_code();
  108.     code = Show_Bits(32);
  109.  
  110.     switch(code)
  111.     {
  112.     case SEQUENCE_HEADER_CODE:
  113.       break;
  114.     case PACK_START_CODE:
  115.       System_Stream_Flag = 1;
  116.     case VIDEO_ELEMENTARY_STREAM:
  117.       System_Stream_Flag = 1;
  118.       break;
  119.     default:
  120.       sprintf(Error_Text,"Unable to recognize stream type %ld\n",code);
  121.       Error(Error_Text);
  122.       break;
  123.     }
  124.  
  125.     fseek(base.Infile, 0, SEEK_SET);
  126.     Initialize_Buffer(); 
  127.   }
  128.  
  129.   if(base.Infile!=0)
  130.   {
  131.     fseek(base.Infile, 0, SEEK_SET);
  132.   }
  133.  
  134.   Initialize_Buffer(); 
  135.  
  136.   if(Two_Streams)
  137.   {
  138.     ld = &enhan; /* select enhancement layer context */
  139.  
  140.     if ((enhan.Infile = fopen(Enhancement_Layer_Bitstream_Filename,"r"))<0)
  141.     {
  142.       sprintf(Error_Text,"enhancment layer bitstream file %s not found\n",
  143.         Enhancement_Layer_Bitstream_Filename);
  144.  
  145.       Error(Error_Text);
  146.     }
  147.  
  148.     Initialize_Buffer();
  149.     ld = &base;
  150.   }
  151.  
  152.   Initialize_Decoder();
  153.  
  154.   ret = Decode_Bitstream();
  155.  
  156.   fclose(base.Infile);
  157.  
  158.   if (Two_Streams)
  159.     fclose(enhan.Infile);
  160.  
  161.   return 0;
  162. }
  163.  
  164. /* IMPLEMENTAION specific rouintes */
  165. static void Initialize_Decoder()
  166. {
  167.   int i;
  168.  
  169.   /* Clip table */
  170.   if (!(Clip=(unsigned char *)malloc(1024)))
  171.     Error("Clip[] malloc failed\n");
  172.  
  173.   Clip += 384;
  174.  
  175.   for (i=-384; i<640; i++)
  176.     Clip[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
  177.  
  178.   /* IDCT */
  179.   if (Reference_IDCT_Flag)
  180.     Initialize_Reference_IDCT();
  181.   else
  182.     Initialize_Fast_IDCT();
  183.  
  184. }
  185.  
  186. /* mostly IMPLEMENTAION specific rouintes */
  187. static void Initialize_Sequence()
  188. {
  189.   int cc, size;
  190.   static int Table_6_20[3] = {6,8,12};
  191.  
  192.   /* check scalability mode of enhancement layer */
  193.   if (Two_Streams && (enhan.scalable_mode!=SC_SNR) && (base.scalable_mode!=SC_DP))
  194.     Error("unsupported scalability mode\n");
  195.  
  196.   /* force MPEG-1 parameters for proper decoder behavior */
  197.   /* see ISO/IEC 13818-2 section D.9.14 */
  198.   if (!base.MPEG2_Flag)
  199.   {
  200.     progressive_sequence = 1;
  201.     progressive_frame = 1;
  202.     picture_structure = FRAME_PICTURE;
  203.     frame_pred_frame_dct = 1;
  204.     chroma_format = CHROMA420;
  205.     matrix_coefficients = 5;
  206.   }
  207.  
  208.   /* round to nearest multiple of coded macroblocks */
  209.   /* ISO/IEC 13818-2 section 6.3.3 sequence_header() */
  210.   mb_width = (horizontal_size+15)/16;
  211.   mb_height = (base.MPEG2_Flag && !progressive_sequence) ? 2*((vertical_size+31)/32)
  212.                                         : (vertical_size+15)/16;
  213.  
  214.   Coded_Picture_Width = 16*mb_width;
  215.   Coded_Picture_Height = 16*mb_height;
  216.  
  217.   /* ISO/IEC 13818-2 sections 6.1.1.8, 6.1.1.9, and 6.1.1.10 */
  218.   Chroma_Width = (chroma_format==CHROMA444) ? Coded_Picture_Width
  219.                                            : Coded_Picture_Width>>1;
  220.   Chroma_Height = (chroma_format!=CHROMA420) ? Coded_Picture_Height
  221.                                             : Coded_Picture_Height>>1;
  222.   
  223.   /* derived based on Table 6-20 in ISO/IEC 13818-2 section 6.3.17 */
  224.   block_count = Table_6_20[chroma_format-1];
  225.  
  226.   for (cc=0; cc<3; cc++)
  227.   {
  228.     if (cc==0)
  229.       size = Coded_Picture_Width*Coded_Picture_Height;
  230.     else
  231.       size = Chroma_Width*Chroma_Height;
  232.  
  233.     if (!(backward_reference_frame[cc] = (unsigned char *)malloc(size)))
  234.       Error("backward_reference_frame[] malloc failed\n");
  235.  
  236.     if (!(forward_reference_frame[cc] = (unsigned char *)malloc(size)))
  237.       Error("forward_reference_frame[] malloc failed\n");
  238.  
  239.     if (!(auxframe[cc] = (unsigned char *)malloc(size)))
  240.       Error("auxframe[] malloc failed\n");
  241.  
  242.     if(Ersatz_Flag)
  243.       if (!(substitute_frame[cc] = (unsigned char *)malloc(size)))
  244.         Error("substitute_frame[] malloc failed\n");
  245.  
  246.  
  247.     if (base.scalable_mode==SC_SPAT)
  248.     {
  249.       /* this assumes lower layer is 4:2:0 */
  250.       if (!(llframe0[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
  251.         Error("llframe0 malloc failed\n");
  252.       if (!(llframe1[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
  253.         Error("llframe1 malloc failed\n");
  254.     }
  255.   }
  256.  
  257.   /* SCALABILITY: Spatial */
  258.   if (base.scalable_mode==SC_SPAT)
  259.   {
  260.     if (!(lltmp = (short *)malloc(lower_layer_prediction_horizontal_size*((lower_layer_prediction_vertical_size*vertical_subsampling_factor_n)/vertical_subsampling_factor_m)*sizeof(short))))
  261.       Error("lltmp malloc failed\n");
  262.   }
  263.  
  264. #ifdef DISPLAY
  265.   if (Output_Type==T_X11)
  266.   {
  267.     Initialize_Display_Process("");
  268.     Initialize_Dither_Matrix();
  269.   }
  270. #endif /* DISPLAY */
  271.  
  272. }
  273.  
  274. #ifdef STORM
  275. void Error(char *text)
  276. #else
  277. void Error(text)
  278. char *text;
  279. #endif
  280. {
  281.   fprintf(stderr,text);
  282.   exit(1);
  283. }
  284.  
  285. /* Trace_Flag output */
  286. #ifdef STORM
  287. void Print_Bits(int code, int bits, int len)
  288. #else
  289. void Print_Bits(code,bits,len)
  290. int code,bits,len;
  291. #endif
  292. {
  293.   int i;
  294.   for (i=0; i<len; i++)
  295.     printf("%d",(code>>(bits-1-i))&1);
  296. }
  297.  
  298. extern int truecolor;
  299. int fpscount=0;
  300. /* option processing */
  301. #ifdef STORM
  302. extern int direct;
  303. static void Process_Options(int argc,char *argv[])
  304. #else
  305. static void Process_Options(argc,argv)
  306. int argc;                  /* argument count  */
  307. char *argv[];              /* argument vector */
  308. #endif
  309. {
  310.   int i, LastArg, NextArg;
  311.  
  312.   /* at least one argument should be present */
  313.   if (argc<2)
  314.   {
  315.     printf("\n%s, %s\n",Version,Author);
  316.     printf("MPEG Audio Decoding Copyright (c) 1991,92 MPEG/Audio Software Simulation Group\n");
  317.     printf("Amiga version and Audio implementation in 1999 by Jesper Svennevid\n");
  318.     printf("Usage:  mpeg2decode {options}\n");
  319. printf("Options: -b  file   main bitstream (base or spatial enhancement layer)\n");
  320. printf("         -cn file   conformance report (n: level)\n");
  321. printf("         -d         Use Direct CGX/P96 Mode\n");
  322. printf("         -e  file   enhancement layer bitstream (SNR or Data Partitioning)\n");
  323. printf("         -f         store/display interlaced video in frame format\n");
  324. printf("         -g         concatenated file format for substitution method (-x)\n");
  325. printf("         -h         double pixel-size\n");
  326. printf("         -in file   information & statistics report  (n: level)\n");
  327. printf("         -j         enable fps counter\n");
  328. printf("         -l  file   file name pattern for lower layer sequence\n");
  329. printf("                    (for spatial scalability)\n");
  330. printf("         -mn        modeid for screenmode(format: -m0x0000, replace with proper ModeID)\n");
  331. printf("         -on file   output format (0:YUV 1:SIF 2:TGA 3:PPM 4:AmigaOS 5:AmigaOS HiQ 6: AmigaOS 24 Bit 7: AmigaOS 24 Bit HiQ)\n");
  332. printf("         -pn        Number of frames to skip(currently a bit slow)\n");
  333. printf("         -q         disable warnings to stderr\n");
  334. printf("         -r         use double precision reference IDCT\n");
  335. printf("         -s         enable decoding of layer 1/2 sound \n");
  336. printf("         -t         enable low level tracing to stdout\n");
  337. printf("         -u  file   print user_data to stdio or file\n");
  338. printf("         -vn        verbose output (n: level)\n");
  339. printf("         -w         Use WB Window Display\n");
  340. printf("         -x  file   filename pattern of picture substitution sequence\n\n");
  341. printf("File patterns:  for sequential filenames, \"printf\" style, e.g. rec%%d\n");
  342. printf("                 or rec%%d%%c for fieldwise storage\n");
  343. printf("Levels:        0:none 1:sequence 2:picture 3:slice 4:macroblock 5:block\n\n");
  344. printf("Example:       mpeg2decodeWOS -o4 -p3 -s -b bitstream.mpg <- decode a bitstream with\n");
  345. printf("                                                          frameskip 3 and sound enabled\n");
  346. printf("         \n");
  347.     exit(0);
  348.   }
  349.  
  350.  
  351.   Output_Type = -1;
  352.   i = 1;
  353.  
  354.   /* command-line options are proceeded by '-' */
  355.  
  356.   direct=0;
  357.   fpscount=0;
  358.   truecolor=0;
  359.   Output_Picture_Filename = "";
  360.   Output_Type=4;
  361.   while(i < argc)
  362.   {
  363.     /* check if this is the last argument */
  364.     LastArg = ((argc-i)==1);
  365.  
  366.     /* parse ahead to see if another flag immediately follows current
  367.        argument (this is used to tell if a filename is missing) */
  368.     if(!LastArg)
  369.       NextArg = (argv[i+1][0]=='-');
  370.     else
  371.       NextArg = 0;
  372.  
  373.     /* second character, [1], after '-' is the switch */
  374.     if(argv[i][0]=='-')
  375.     {
  376.       switch(toupper(argv[i][1]))
  377.       {
  378.         /* third character. [2], is the value */
  379.       case 'B':
  380.         Main_Bitstream_Flag = 1;
  381.  
  382.         if(NextArg || LastArg)
  383.         {
  384.           printf("ERROR: -b must be followed the main bitstream filename\n");
  385.     }
  386.         else
  387.           Main_Bitstream_Filename = argv[++i]; 
  388.  
  389.         break;
  390.  
  391.  
  392.       case 'C':
  393.  
  394. #ifdef VERIFY
  395.         Verify_Flag = atoi(&argv[i][2]); 
  396.  
  397.         if((Verify_Flag < NO_LAYER) || (Verify_Flag > ALL_LAYERS))
  398.         {
  399.           printf("ERROR: -c level (%d) out of range [%d,%d]\n",
  400.             Verify_Flag, NO_LAYER, ALL_LAYERS);
  401.           exit(ERROR);
  402.         }
  403. #else  /* VERIFY */
  404.         printf("This program not compiled for Verify_Flag option\n");
  405. #endif /* VERIFY */
  406.         break;
  407.  
  408.       case 'D':
  409.         direct=1;
  410.         printf("Direct Video RAM Mode enabled.\n");
  411.         break;
  412.       case 'E':
  413.         Two_Streams = 1; /* either Data Partitioning (DP) or SNR Scalability enhancment */
  414.                        
  415.         if(NextArg || LastArg)
  416.         {
  417.           printf("ERROR: -e must be followed by filename\n");
  418.           exit(ERROR);
  419.         }
  420.         else
  421.           Enhancement_Layer_Bitstream_Filename = argv[++i]; 
  422.  
  423.         break;
  424.  
  425.  
  426.       case 'F':
  427.         Frame_Store_Flag = 1;
  428.         break;
  429.  
  430.       case 'G':
  431.         Big_Picture_Flag = 1;
  432.         break;
  433.  
  434.       case 'H':
  435.     Double_Pixels = 2L;
  436.     break;
  437.  
  438.       case 'I':
  439. #ifdef VERIFY
  440.         Stats_Flag = atoi(&argv[i][2]); 
  441. #else /* VERIFY */
  442.         printf("WARNING: This program not compiled for -i option\n");
  443. #endif /* VERIFY */     
  444.         break;
  445.       case 'J': fpscount=1;break;
  446.       case 'L':  /* spatial scalability flag */
  447.         Spatial_Flag = 1;
  448.  
  449.        if(NextArg || LastArg)
  450.        {
  451.          printf("ERROR: -l must be followed by filename\n");
  452.          exit(ERROR);
  453.        }
  454.        else
  455.          Lower_Layer_Picture_Filename = argv[++i]; 
  456.  
  457.         break;
  458.  
  459.       case 'M':
  460.  
  461.         if(!sscanf(&argv[i][2],"%lx",&Mode_ID))
  462.         Mode_ID = INVALID_ID;
  463.  
  464.         break;
  465.  
  466.       case 'O':
  467.   
  468.         Output_Type = atoi(&argv[i][2]); 
  469.         truecolor=0;
  470.         if (Output_Type==6)
  471.         {
  472.          Output_Type=4;
  473.          truecolor=1;
  474.         }
  475.         else if (Output_Type==7)
  476.         {
  477.          Output_Type=5;
  478.          truecolor=1;
  479.         }
  480.         if((Output_Type==4) || (Output_Type==5))
  481.           Output_Picture_Filename = "";  /* no need of filename */
  482.         else if(NextArg || LastArg)  
  483.         {
  484.           printf("ERROR: -o must be followed by filename\n");
  485.           exit(ERROR);
  486.         }
  487.         else
  488.         /* filename is separated by space, so it becomes the next argument */
  489.           Output_Picture_Filename = argv[++i]; 
  490.  
  491. #ifdef DISPLAY
  492.         if (Output_Type==T_X11HIQ)
  493.         {
  494.           hiQdither = 1;
  495.           Output_Type=T_X11;
  496.         }
  497. #endif /* DISPLAY */
  498.         break;
  499.  
  500.       case 'P':
  501.         Frame_Skip = atoi(&argv[i][2]); 
  502.     if((Frame_Skip>9)||(Frame_Skip<1)) Frame_Skip = 0;
  503.         break;
  504.  
  505.       case 'Q':
  506.         Quiet_Flag = 1;
  507.         break;
  508.  
  509.       case 'R':
  510.         Reference_IDCT_Flag = 1;
  511.         break;
  512.     
  513.       case 'S':
  514.  
  515.     Enable_Sound = 1L;
  516.  
  517.         break;
  518.  
  519.       case 'T':
  520. #ifdef TRACE
  521.         Trace_Flag = 1;
  522. #else /* TRACE */
  523.         printf("WARNING: This program not compiled for -t option\n");
  524. #endif /* TRACE */
  525.         break;
  526.  
  527.       case 'U':
  528.         User_Data_Flag = 1;
  529.  
  530.       case 'V':
  531. #ifdef VERBOSE
  532.         Verbose_Flag = atoi(&argv[i][2]); 
  533. #else /* VERBOSE */
  534.         printf("This program not compiled for -v option\n");
  535. #endif /* VERBOSE */
  536.         break;
  537.  
  538.       case 'W': wb=1;break;
  539.       case 'X':
  540.         Ersatz_Flag = 1;
  541.  
  542.        if(NextArg || LastArg)
  543.        {
  544.          printf("ERROR: -x must be followed by filename\n"); 
  545.          exit(ERROR);
  546.        }
  547.        else
  548.         Substitute_Picture_Filename = argv[++i]; 
  549.  
  550.         break;
  551.  
  552.  
  553.  
  554.       default:
  555.         fprintf(stderr,"undefined option -%c ignored. Exiting program\n", 
  556.           argv[i][1]);
  557.  
  558.         exit(ERROR);
  559.     
  560.       } /* switch() */
  561.     } /* if argv[i][0] == '-' */
  562.     
  563.     i++;
  564.  
  565.     /* check for bitstream filename argument (there must always be one, at the very end
  566.        of the command line arguments */
  567.  
  568.   } /* while() */
  569.  
  570.   if (direct&&truecolor) direct=0;
  571.   if (wb&&wb) direct=0;
  572.   /* options sense checking */
  573.  
  574.   if(Main_Bitstream_Flag!=1)
  575.   {
  576.     printf("There must be a main bitstream specified (-b filename)\n");
  577.     exit(0);
  578.   }
  579.  
  580.   /* force display process to show frame pictures */
  581.   if((Output_Type==4 || Output_Type==5) && Frame_Store_Flag)
  582.     Display_Progressive_Flag = 1;
  583.   else
  584.     Display_Progressive_Flag = 0;
  585.  
  586. #ifdef VERIFY
  587.   /* parse the bitstream, do not actually decode it completely */
  588.   
  589.  
  590. #if 0
  591.   if(Output_Type==-1)
  592.   {
  593.     Decode_Layer = Verify_Flag;
  594.     printf("FYI: Decoding bitstream elements up to: %s\n", 
  595.       Layer_Table[Decode_Layer]);
  596.   }
  597.   else
  598. #endif
  599.     Decode_Layer = ALL_LAYERS;
  600.  
  601. #endif /* VERIFY */
  602.  
  603.   /* no output type specified */
  604.   if(Output_Type==-1)
  605.   {
  606.     Output_Type = 9; 
  607.     Output_Picture_Filename = "";
  608.   }
  609.  
  610.  
  611. #ifdef DISPLAY
  612.   if (Output_Type==T_X11)
  613.   {
  614.     if(Frame_Store_Flag)
  615.       Display_Progressive_Flag = 1;
  616.     else
  617.       Display_Progressive_Flag = 0;
  618.  
  619.     Frame_Store_Flag = 1; /* to avoid calling dither() twice */
  620.   }
  621. #endif
  622.  
  623.  
  624. }
  625.  
  626.  
  627. #ifdef OLD
  628. /* 
  629.    this is an old routine used to convert command line arguments
  630.    into integers 
  631. */
  632. #ifdef STORM
  633. static int Get_Val(char *argv[])
  634. #else
  635. static int Get_Val(argv)
  636. char *argv[];
  637. #endif
  638. {
  639.   int val;
  640.  
  641.   if (sscanf(argv[1]+2,"%d",&val)!=1)
  642.     return 0;
  643.  
  644.   while (isdigit(argv[1][2]))
  645.     argv[1]++;
  646.  
  647.   return val;
  648. }
  649. #endif
  650.  
  651.  
  652.  
  653. static int Headers()
  654. {
  655.   int ret;
  656.  
  657.   ld = &base;
  658.   
  659.  
  660.   /* return when end of sequence (0) or picture
  661.      header has been parsed (1) */
  662.  
  663.   ret = Get_Hdr();
  664.  
  665.  
  666.   if (Two_Streams)
  667.   {
  668.     ld = &enhan;
  669.     if (Get_Hdr()!=ret && !Quiet_Flag)
  670.       fprintf(stderr,"streams out of sync\n");
  671.     ld = &base;
  672.   }
  673.  
  674.   return ret;
  675. }
  676.  
  677.  
  678.  
  679. static int Decode_Bitstream()
  680. {
  681.   int ret;
  682.   int Bitstream_Framenum;
  683.  
  684.   Bitstream_Framenum = 0;
  685.  
  686.   for(;;)
  687.   {
  688.  
  689. #ifdef VERIFY
  690.     Clear_Verify_Headers();
  691. #endif /* VERIFY */
  692.  
  693.     ret = Headers();
  694.     
  695.     if(ret==1)
  696.     {
  697.       ret = video_sequence(&Bitstream_Framenum);
  698.     }
  699.     else
  700.       return(ret);
  701.   }
  702.  
  703. }
  704.  
  705.  
  706. static void Deinitialize_Sequence()
  707. {
  708.   int i;
  709.  
  710.   /* clear flags */
  711.   base.MPEG2_Flag=0;
  712.  
  713.   for(i=0;i<3;i++)
  714.   {
  715.     free(backward_reference_frame[i]);
  716.     free(forward_reference_frame[i]);
  717.     free(auxframe[i]);
  718.  
  719.     if (base.scalable_mode==SC_SPAT)
  720.     {
  721.      free(llframe0[i]);
  722.      free(llframe1[i]);
  723.     }
  724.   }
  725.  
  726.   if (base.scalable_mode==SC_SPAT)
  727.     free(lltmp);
  728.  
  729. #ifdef DISPLAY
  730.   if (Output_Type==T_X11) 
  731.     Terminate_Display_Process();
  732. #endif
  733. }
  734.  
  735. #ifdef STORM
  736. static int video_sequence(int *Bitstream_Framenumber)
  737. #else
  738. static int video_sequence(Bitstream_Framenumber)
  739. int *Bitstream_Framenumber;
  740. #endif
  741. {
  742.   extern unsigned long audioBufferSize;
  743.   int Bitstream_Framenum;
  744.   int Sequence_Framenum;
  745.   int Return_Value;
  746.  
  747.   Bitstream_Framenum = *Bitstream_Framenumber;
  748.   Sequence_Framenum=0;
  749.  
  750.   Initialize_Sequence();
  751.  
  752.   /* decode picture whose header has already been parsed in 
  753.      Decode_Bitstream() */
  754.  
  755.  
  756.   Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
  757.   Decode_Audio();
  758.  
  759.   /* update picture numbers */
  760.   if (!Second_Field)
  761.   {
  762.     Bitstream_Framenum++;
  763.     Sequence_Framenum++;
  764.   }
  765.  
  766.   /* loop through the rest of the pictures in the sequence */
  767.   while ((Return_Value=Headers()))
  768.   {
  769.     Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
  770.     Decode_Audio();
  771.     while(audioBufferSize>5120) Decode_Audio();
  772.  
  773.     if (!Second_Field)
  774.     {
  775.       Bitstream_Framenum++;
  776.       Sequence_Framenum++;
  777.     }
  778.   }
  779.  
  780.   /* put last frame */
  781.   if (Sequence_Framenum!=0)
  782.   {
  783.     Output_Last_Frame_of_Sequence(Bitstream_Framenum);
  784.     while(Decode_Audio());
  785.   }
  786.  
  787.   Deinitialize_Sequence();
  788.  
  789. #ifdef VERIFY
  790.     Clear_Verify_Headers();
  791. #endif /* VERIFY */
  792.  
  793.   *Bitstream_Framenumber = Bitstream_Framenum;
  794.   return(Return_Value);
  795. }
  796.  
  797.  
  798.  
  799. static void Clear_Options()
  800. {
  801.   Verbose_Flag = 0;
  802.   Output_Type = 0;
  803.   Output_Picture_Filename = " ";
  804.   hiQdither  = 0;
  805.   Output_Type = 0;
  806.   Frame_Store_Flag = 0;
  807.   Spatial_Flag = 0;
  808.   Lower_Layer_Picture_Filename = " ";
  809.   Reference_IDCT_Flag = 0;
  810.   Trace_Flag = 0;
  811.   Quiet_Flag = 0;
  812.   Ersatz_Flag = 0;
  813.   Substitute_Picture_Filename  = " ";
  814.   Two_Streams = 0;
  815.   Enhancement_Layer_Bitstream_Filename = " ";
  816.   Big_Picture_Flag = 0;
  817.   Main_Bitstream_Flag = 0;
  818.   Main_Bitstream_Filename = " ";
  819.   Verify_Flag = 0;
  820.   Stats_Flag  = 0;
  821.   User_Data_Flag = 0; 
  822.   Mode_ID = INVALID_ID;
  823.   Enable_Sound = 0L;
  824.   Double_Pixels = 1L;
  825. }
  826.  
  827.  
  828. #ifdef DEBUG
  829. static void Print_Options()
  830. {
  831.   
  832.   printf("Verbose_Flag                         = %d\n", Verbose_Flag);
  833.   printf("Output_Type                          = %d\n", Output_Type);
  834.   printf("Output_Picture_Filename              = %s\n", Output_Picture_Filename);
  835.   printf("hiQdither                            = %d\n", hiQdither);
  836.   printf("Output_Type                          = %d\n", Output_Type);
  837.   printf("Frame_Store_Flag                     = %d\n", Frame_Store_Flag);
  838.   printf("Spatial_Flag                         = %d\n", Spatial_Flag);
  839.   printf("Lower_Layer_Picture_Filename         = %s\n", Lower_Layer_Picture_Filename);
  840.   printf("Reference_IDCT_Flag                  = %d\n", Reference_IDCT_Flag);
  841.   printf("Trace_Flag                           = %d\n", Trace_Flag);
  842.   printf("Quiet_Flag                           = %d\n", Quiet_Flag);
  843.   printf("Ersatz_Flag                          = %d\n", Ersatz_Flag);
  844.   printf("Substitute_Picture_Filename          = %s\n", Substitute_Picture_Filename);
  845.   printf("Two_Streams                          = %d\n", Two_Streams);
  846.   printf("Enhancement_Layer_Bitstream_Filename = %s\n", Enhancement_Layer_Bitstream_Filename);
  847.   printf("Big_Picture_Flag                     = %d\n", Big_Picture_Flag);
  848.   printf("Main_Bitstream_Flag                  = %d\n", Main_Bitstream_Flag);
  849.   printf("Main_Bitstream_Filename              = %s\n", Main_Bitstream_Filename);
  850.   printf("Verify_Flag                          = %d\n", Verify_Flag);
  851.   printf("Stats_Flag                           = %d\n", Stats_Flag);
  852.   printf("User_Data_Flag                       = %d\n", User_Data_Flag);
  853.   printf("Mode_ID                              = %d\n", Mode_ID);
  854.  
  855. }
  856. #endif
  857.